// pages/item/item.js

var networkTool = require("../../utils/networkTool.js");
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchinfo: {},
    datainfo: [],
    isEmpty: true,
    totalCount: 0,
    formatType: true,
    formatImg: "/images/icon/viewlist.png",
    isData: true,  //标示是否有数据
    searchValue: null,
    isLoadingMore: false,
    isLoading: true  //是否需要加载数据
  },

  searchValue: function (e) {
    this.data.searchValue = e.detail.value;
  },


  /**
   * 加载数据
   */
  loadData: function () {
    networkTool.searchItem(this.data.searchinfo, this.bindData);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      searchinfo: app.globalData.searchInfo,
      searchValue: app.globalData.searchInfo.ST_SCONDITION
    });
    this.loadData(); 
       */
  },

  /**
   * 绑定数据
   */
  bindData: function (resp) {
    var items = [];
    for (var idx in resp.VIPCONTENT) {
      var item = resp.VIPCONTENT[idx];
      var name = item.ST_NAME;

      var temp = {
        ST_CODE: item.ST_CODE,
        ST_NAME: name,
        ST_IMG: item.ST_IMG,
        ST_CURR: item.ST_CURR,
        NO_PRICE: item.NO_PRICE,
        ST_PRICETIPS: item.ST_PRICETIPS,
        NO_SOURCEPRICE: item.NO_SOURCEPRICE,
        NO_ISNOWTL: item.NO_ISNOWTL,
        DT_NOW: item.DT_NOW,
        DT_TL_END: item.DT_TL_END,
        DT_TL_START: item.DT_TL_START,
      }
      items.push(temp);
    }
    var totalItems = {};

    if (!this.data.isEmpty) {
      totalItems = this.data.datainfo.concat(items);
    } else {
      totalItems = (items);
      this.data.isEmpty = false;
    }

    this.setData({
      datainfo: totalItems
    });

    //是否显示暂无数据
    var isData = true;
    if (resp.VIPCONTENT) {
      this.data.totalCount += resp.VIPCONTENT.length;
      if (this.data.totalCount > 0) {
        isData = true;
      } else {
        isData = false;
      }
    } else {
      if (this.data.totalCount > 0) {
        isData = true;
      } else {
        isData = false;
      }
    }
    this.setData({
      isData: isData,
      isLoadingMore: false
    });
    wx.hideNavigationBarLoading();
    wx.hideLoading()
  },

  onFormatTap: function (event) {
    this.setData({
      formatType: !this.data.formatType,
      formatImg: !this.data.formatType ? "/images/icon/viewlist.png" : "/images/icon/viewGallery.png"
    });
  },

  onFilterTap: function (event) {
    app.globalData.searchInfo = this.data.searchinfo;
    wx.navigateTo({
      url: '/pages/filter/filter',
    })
  },
  //回车确认
  onBindConfirm: function (event) {
    this.searchData(event.detail.value);
  },
  onSearchConfirm: function (event) {
    this.searchData(this.data.searchValue);
  },
  /**
   * 查询数据
   */
  searchData: function (value) {
    wx.showLoading({
      title: '加载中...',
    })
    app.globalData.searchInfo = {
      ST_SCONDITION: value,
      ST_LABEL: "",
      ST_FILTERFIELD: "",
      ST_SEQUENCE: "",
      NO_PAGE: 1.0
    };
    this.setData({
      searchinfo: app.globalData.searchInfo,
      datainfo: []
    });
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isLoading) {
      wx.showLoading({
        title: '加载中...',
      })
      this.setData({
        datainfo: [],
        isLoading:false,
        searchinfo: app.globalData.searchInfo,
        searchValue: app.globalData.searchInfo.ST_SCONDITION
      });
      this.loadData();

    }
  },

  ckItemTap:function(event){
    var code = (event.currentTarget.dataset.code);
    wx.navigateTo({
      url: '/pages/item/detail/detail?code='+code,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    /**
    this.data.searchinfo.NO_PAGE = 1;
    this.data.totalCount = 0;
    this.data.isEmpty = true;
    this.data.datainfo = [];
    networkTool.searchItem(this.data.searchinfo, this.bindData);
    wx.showNavigationBarLoading();
    */
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.searchinfo.NO_PAGE = this.data.searchinfo.NO_PAGE + 1;
    wx.showNavigationBarLoading();
    this.setData({
      isLoadingMore: true
    });

    //延迟加载
    setTimeout(this.loadData, 300);

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})